package com.ynnz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ynnz.pojo.Customer;
import com.ynnz.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Title: CustomerController
 * @Description:
 * @PACKAGE_NAME：com.ynnz.controller
 * @Author: 94358 fangjinsen
 * CreateDate: 2022/5/9 10:53
 */
@Controller
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 模糊查询
     * @param model
     * @param customer
     * @param start
     * @param size
     * @return
     */
    @RequestMapping("/queryCust")
    @ResponseBody
    public String queryCustomer(Model model,Customer customer,int start ,int size) {


        List<Customer> customers = customerService.queryCust(customer,start,size);
        model.addAttribute("customers", customers);
        return JSON.toJSONString(customers);
        //return "main.jsp";
    }

    /**
     * 查询所有-测试接口
     * @param model
     * @return
     */
    @RequestMapping("/queryCustomer")
    @ResponseBody
    public String queryCustomer(Model model) {
        List<Customer> customers = customerService.queryCustomer();
        model.addAttribute("customers", customers);
        return JSON.toJSONString(customers);
        //return "main.jsp";
    }

    @RequestMapping(value = "/deleteCustomer")
    @ResponseBody
    public String deleteCustomer(String id)  {
        System.out.println(id);
        String[] split = id.split(",");
        //校验是否全部删除的初始变量
        int indexs = 0;
        for (String s : split) {
            System.out.println("当前获取到的ID为："+s);
            int i = customerService.deleteCustomer(Integer.parseInt(s));
            indexs++;
            if (i>0){
                System.out.println("成功删除id为："+s);
            }
        }
        //全部删除返回succes
        if (split.length==indexs){
            return "success";
        }
        if (split.length!=indexs){
            return "error";
        }
        return null;
        //return "main.jsp";
    }

    @RequestMapping(value = "/insertCustomer")
    @ResponseBody
    public String insertCustomer(@RequestBody String insertCust)  {
        //获取当前系统时间
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        //获取前端数据
        JSONObject jsonObject = JSON.parseObject(insertCust);
        String insertCustomername = jsonObject.getString("insertCustomername");
        String insertCustSourceValue = jsonObject.getString("insertCustSourceValue");
        String insertCustIndustryValue = jsonObject.getString("insertCustIndustryValue");
        String insertCustPhone = jsonObject.getString("insertCustPhone");
        String insertCustMobile = jsonObject.getString("insertCustMobile");
        String insertCustAddress = jsonObject.getString("insertCustAddress");
        String format = dtf.format(now);
        System.out.println(insertCustomername);
        Customer customer = new Customer(null,
                insertCustomername,
                1,
                1,
                insertCustSourceValue,
                insertCustIndustryValue,
                insertCustPhone,
                insertCustMobile,
                insertCustAddress,
                dtf.format(now));
        int i = customerService.insertCustomer(customer);
        if (i>0){
            System.out.println("新增成功");
            return "success";
        }
        System.out.println("调用成功");
        return null;
        //return "main.jsp";
    }


}
